define(
	[
		'require'
	],
	function(require){
		var Controller = Ember.Controller.extend({
			keyUpEnent: function(){
				var ctrl = this;
				setTimeout(function(){
					$("#mouseKeyUp").on("keyup",function(event){
						if(event.keyCode == 13){
							ctrl.allServerResponse();
						};
					});
				},0)
			},
			serviceValueChange: Ember.observer("serviceValue",function(){
				this.allServerResponse();
			}),
			needs: ["serviceServer"],

			serviceType: Ember.A([]),

			getserviceType: function() {
				var me = this;
				return this.store.ajax("GET", "protocols/L7").then(function(response){
		   	 		if (response.protocols) {
		   	 			var arr = [];
		   	 			Ember.EnumerableUtils.forEach(response.protocols, function(item) {
		   	 				arr.push({displaytext: item.name, value: item.l7_proto, data: item})
		   	 			});
		   	 			me.set("serviceType", Ember.A(arr));
		   	 		}
		   		});
			},
			pageSize: 10,
			serverColumns: Ember.computed(function(){
				var columns =  [
					{
						headerText: "服务名",
						dataKey:"name",
						allowSort: false,
						cellFormatter: function (args, colume) {
							if (args.row.type & $.wijmo.wijgrid.rowType.data) {
								var ctrl = this.get("controller");
								var data = args.row.data.name;
								args.$container.text(data);
								return true;
							}
						}
						
					},
					{
						headerText: "IP地址",
						dataKey:"ip",
						allowSort: true,
						cellFormatter: function (args, colume) {
							if (args.row.type & $.wijmo.wijgrid.rowType.data) {

								var ctrl = this.get("controller");
								var data = args.row.data.ip;
								args.$container.text(data);
								return true;
							}
						}
					},
					{
						headerText: "端口",
						dataKey:"port",
						allowSort: true,
						cellFormatter: function (args, colume) {
							if (args.row.type & $.wijmo.wijgrid.rowType.data) {
								var ctrl = this.get("controller");
								var data = args.row.data.port;
								args.$container.text(data);
								return true;
							}
						}
					},
					{
						headerText: "服务类型",
						dataKey:"protocolName",
						allowSort: true,
						cellFormatter: function (args, colume) {
							if (args.row.type & $.wijmo.wijgrid.rowType.data) {
								var ctrl = this.get("controller");
								var data = args.row.data.protocolName;
								if(!data){
									data = '-';
								}
								args.$container.text(data);
								return true;
							}
						}
					},
					{
						headerText: "吞吐量",
						dataKey:"throughput",
						allowSort: true,
						cellFormatter: function (args, colume) {
							if (args.row.type & $.wijmo.wijgrid.rowType.data) {
								var ctrl = this.get("controller");
								var data = args.row.data.throughput;
								if(data == 0){
									 data = 0;
								}else if(!data){
									data = '-';
	 							}
								args.$container.text(data);
								return true;
							}
						}
					},
					{
						headerText: "平均响应时间",
						dataKey:"response",
						allowSort: true,
						cellFormatter: function (args, colume) {
							if (args.row.type & $.wijmo.wijgrid.rowType.data) {
								var ctrl = this.get("controller");
								var data = args.row.data.response;
								data = APM.util.indicatorFormatter("transform_time")(data);
								if(data == 0){
									 data = 0;
								}else if(!data){
									data = '-';
	 							}
	 							var cb = $("<div class='date-text line'></div>");
	 							cb.text(data);
								cb.appendTo(args.$container);
								return true;
							}
						}
					},
					{
						headerText: "错误",
						dataKey:"error",
						allowSort: true,
						cellFormatter: function (args, colume) {
							if (args.row.type & $.wijmo.wijgrid.rowType.data) {
								var ctrl = this.get("controller");
								var data = args.row.data.error;
								if(data == 0){
									 data = 0;
								}else if(!data){
									data = '-';
	 							}
								var cb = $("<div class='date-text line'></div>").text(data);
								cb.appendTo(args.$container);
								return true;
							}
						}
					},
					{
						headerText: "最后更新",
						dataKey:"timestamp",
						allowSort: true,
						cellFormatter: function (args, colume) {
							if (args.row.type & $.wijmo.wijgrid.rowType.data) {
								var ctrl = this.get("controller");
								var date = new Date(args.row.data.timestamp);
								var dateFor=APM.util.formatDate(date, "yyyy-MM-dd H:mm:ss");
								if(!(args.row.data.timestamp)){
									dateFor = '-';
								}
								var cb = $("<div class='date-text line'></div>").text(dateFor);
								cb.appendTo(args.$container);
								return true;
							}
						}
					}
				];
				if(!this.get("auth.isNormal")){
					if(!(this.get("auth.user").role == 1)){
					    columns.push({
							headerText: "操作",
							dataKey:"focus",
							allowSort: false,
							cellFormatter: function (args, colume) {
								if (args.row.type & $.wijmo.wijgrid.rowType.data) {
									var ctrl = this.get("controller");
									var date = args.row.data.focus;
									var cb = $("<button class='btn service_btn_width btn-primary'></button>");
									// var btn = $("<button class='btn'></button>");
									if(date){
							    		date = "取消关注";
							    		cb.text(date);
							    	}else{
							    		date = "关注";
							    		cb.text(date);
							    	}
									
									cb.click(function(){
										clearTimeout(ctrl.TimeFn);
									    //执行延时
									  
									    ctrl.TimeFn = setTimeout(function(){
									    	var serviceId = args.row.data.id;
									    	if(date == "关注"){

									    		ctrl.focusRequest(serviceId,{"focus":1})
									    		.then(function(response){
									    			ctrl.allServerResponse();
									    				 ctrl.serviceCountRequest();
									    		   
									    		});
									    		// date = "关注";
									    		// cb.text(date);
									    	}else{
									    		ctrl.focusRequest(serviceId,{"focus":0})
									    		.then(function(response){
									    			ctrl.allServerResponse();
									    				 ctrl.serviceCountRequest();
									    		});
									    		// date = "取消关注";
									    		// cb.text(date);
									    	}
									    },200);
							
									});
									cb.appendTo(args.$container);
									return true;
								}
							}
						});
					}
				};
				return columns;
			}),
			serviceCountRequest: function(){
				var ctrl = this;
				return this.store.ajax("GET","nodes/serviceFocusCount")
				.then(function(response){
					var serviceFocusCount = response.serviceFocusCount;
					ctrl.set("serviceFocus",serviceFocusCount.serviceFocus);
					ctrl.set("serviceAll",serviceFocusCount.serviceAll);
					return serviceFocusCount;
				});
				
			},

			focusRequest: function(serviceId,params){
				return this.store.ajax("PUT","nodes/"+serviceId+"/service/focus",params);
			},
			dialogButtons: {
				ok: {
					text: "确定",
					"class": "btn btn-primary",
					click: function() {
						var ctrl = this.get("controller"),
							serverName = ctrl.get("serverName"),
							dialog = Broada.getView("serviceAllServer");
							
						if(ctrl.get("serverName")){
						 	ctrl.allServerResponse();
						}
						
						dialog.closeDialog();
					}
				},
				cancel: {
					text: "取消",
					"class": "btn",
					click: function() {
						this.closeDialog();
					}
				}
			},
			alerts: Ember.A(),
			modelChange: Ember.observer("serviceFocus",function(){
				this.allServerResponse();
			}),
			
			allServerResponse: function(){
				var ctrl = this;
				var startDate, endDate = new Date(new Date() - timeD_value);
				endDate.setSeconds(0);
				endDate.setMilliseconds(0);
				startDate = new Date(+endDate);
				startDate.setHours(startDate.getHours()-1);
				var params = {
					focus: "0",
					ip: this.get("serviceIP"),
					serviceType: this.get("serviceType"),
					pageIndex: this.get("pageIndex"),
					startDate: +startDate,
					endDate: +endDate,
					pageSize: this.get("pageSize"),
					serviceType: this.get("serviceValue"),
					"orderBy": this.get("orderBy"),
					"desc": this.get("desc")
				};

				if( isLocalDebugModel ){
					params = {
						"focus" : "0"
					};
				}
				return this.store.ajax("GET", "nodes/service",params)
				.then(function(response){
					 var nodes = Ember.A(response.nodes),
					 	 responseTime = "system/response_time/avg",
					 	 throughput = "system/throughput/total",
					 	 error = "system/error/count",
					 	 serverName = ctrl.get("serverName");

					 	 nodes.forEach(function(node,index){
					 	 	if(!node.performance){
					 	 		return;
					 	 	}
					 	 	node.timestamp = node.performance.timestamp;
					 	 	node.response = node.performance[responseTime];
					 	 	node.throughput = node.performance[throughput];
					 	 	node.error = node.performance[error];
					 	 });
					 if (nodes.length == 0) {
						 ctrl.set("pageIndex",0);
						 ctrl.set("pageSize",10);
						 ctrl.set("totalRows",0);
					 } else {
					     ctrl.set("pageIndex",response.meta.pageIndex);
						 ctrl.set("pageSize",response.meta.pageSize);
					 	 ctrl.set("totalRows",response.meta.total);
					 }

					 ctrl.set("alerts",nodes);
				})
			},
			actions: {
				pageIndexChanged: function(e, data, view){
					var pageNo = data.newPageIndex;
					this.set("pageIndex", pageNo);
					this.allServerResponse();
				},
				gridSorted: function(e, data, view){
					view.ensureControl(false);
					var dataKey = data.column.dataKey;
					switch(dataKey){
						case "response":
						dataKey = "system/response_time/avg";
						break;
						case "error":
						dataKey = "system/error/count";
						break;
						case "throughput":
						dataKey = "system/throughput/total";
						break;
						case "successRate":
						dataKey = "system/success/rate";
						break;
					}
					this.set("orderBy", dataKey);
					this.set("desc", data.sortDirection=="descending"||undefined);
					this.allServerResponse();
				},
				search: function(){
					var serviceIP = this.get("serviceIP");
					if(serviceIP){
			   			var reg1=/^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))|(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])|((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))|((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))$/;
			   			if(!reg1.test(serviceIP)){  
			   			   alert("对不起，您输入的查询IP地址有误，请重新输入!");
			   			   return false;
			   			}  
			   		}
					this.allServerResponse();
				}
			}
		});
	
		APM.ServiceServerAllserverController = Controller;

		return Controller;
	}
);	